package cn.jiedh.app.meiweidk.Utils.UI;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * @name：
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/24
 */

public class GridItemDecoration extends RecyclerView.ItemDecoration {
    private int margin;

    public GridItemDecoration(int margin) {
        this.margin = margin;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                               RecyclerView.State state) {
        //列数
        int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        //判断是否是最后一列
        if ((parent.getChildAdapterPosition(view) + 1) % spanCount == 0) {
            outRect.right = margin;
        }
        int childCount = parent.getAdapter().getItemCount();
        //判断是否是最后一行
        if (parent.getChildAdapterPosition(view) + 1   == childCount) {
            outRect.bottom = margin;
        }
        outRect.left = margin;
        outRect.top = margin;
    }
}
